package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC609 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<List<String>> findDuplicate(String[] paths) {
            Map<String, List<String>> map = new HashMap<>();
            for (String path : paths) {
                String[] pathEle = path.split(" ");
                String prefix = pathEle[0];
                for (int i = 1; i < pathEle.length; i++) {
                    String content = pathEle[i].split("\\(")[1].split("\\)")[0];
                    String fullPath = prefix + "/" + pathEle[i].split("\\(")[0];
                    if (map.containsKey(content)) map.get(content).add(fullPath);
                    else {
                        List<String> pathList = new ArrayList<>();
                        pathList.add(fullPath);
                        map.put(content, pathList);
                    }
                }
            }

            List<List<String>> res = new ArrayList<>();
            for (List<String> list : map.values()) {
                if (list.size() > 1) res.add(list);
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        String[] arr = {"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"};
        System.out.println(solution.findDuplicate(arr));
    }

    public static void main(String[] args) throws Exception {
        LC609 an = new LC609();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
